package com.fanghaoran.notes.dao.impl;

import com.fanghaoran.notes.bean.OrderDetail;
import com.fanghaoran.notes.bean.RefundDetail;
import com.fanghaoran.notes.bean.SellDetail;
import com.fanghaoran.notes.dao.OrderDao;
import com.fanghaoran.notes.entity.Item;
import com.fanghaoran.notes.entity.Orders;
import com.fanghaoran.notes.entity.Refund;
import com.fanghaoran.notes.util.BaseDaoUtil;

import java.sql.SQLException;
import java.util.List;

/**
 * @Author: win11
 * @Date: 2023/04/23/9:55
 * @Description: 订单dao层实现类
 */
public class OrderDaoImpl implements OrderDao {
    @Override
    public Integer insertOrder(Orders order) throws SQLException {
        String sql="insert into orders values(?,?,?,?,?);";
        return BaseDaoUtil.executeUpdate(sql,order.getOId(),order.getUId(),order.getOCount(),order.getOTime(),order.getOState());
    }

    @Override
    public Integer insertItem(Item item) throws SQLException {
        String sql="insert into item (o_id,p_id,i_num,i_count) values(?,?,?,?);";
        return BaseDaoUtil.executeUpdate(sql,item.getOId(),item.getPId(),item.getINum(),item.getICount());
    }

    @Override
    public List<Orders> selectOrderByUid(Integer uId) throws Exception {
        String sql="select * from orders where u_id=?;";
        return BaseDaoUtil.executeQuery(Orders.class,sql,uId);
    }

    @Override
    public List<OrderDetail> selectOrderDetail(String oId) throws Exception {
        String sql="select o.o_id as oid,o.o_time as time,o.o_state as state,o.o_count as total_count,p.p_id as pid,p.p_image as image," +
                "p.p_name as name,p.price as price,p.stock,p.sales,p.u_id,i.i_id as iid,i.i_num as num,i.i_count as count from item as i " +
                "join product as p on i.p_id=p.p_id join orders as o on i.o_id=o.o_id where i.o_id=?;";
        return BaseDaoUtil.executeQuery(OrderDetail.class,sql,oId);
    }

    @Override
    public Integer updateState(String oId,int oState) throws SQLException {
        String sql="update orders set o_state=? where o_id=?;";
        return BaseDaoUtil.executeUpdate(sql,oState,oId);
    }

    @Override
    public List<SellDetail> selectSellByUid(Integer uId) throws Exception {
        String sql = "select o.o_id,o.o_time,u.username as buyer,p.p_name,i.i_num,i.i_count,o.o_state " +
                "from item as i join product as p on i.p_id=p.p_id " +
                "join orders as o on i.o_id=o.o_id " +
                "join user as u on o.u_id=u.u_id where p.u_id=?; ";
        return BaseDaoUtil.executeQuery(SellDetail.class,sql,uId);
    }

    @Override
    public List<Refund> selectRefundByIid(int iid) throws Exception {
        String sql = "select * from refund where i_id=?;";
        return BaseDaoUtil.executeQuery(Refund.class,sql,iid);
    }

    @Override
    public Integer insertRefund(int iid, String reason, int state, int uId) throws SQLException {
        String sql = "insert into refund(i_id,u_id,reason,r_state) values(?,?,?,?);";
        return BaseDaoUtil.executeUpdate(sql,iid,uId,reason,state);
    }

    @Override
    public List<RefundDetail> selectRefundByUid(int uId) throws Exception {
        String sql = "select r.i_id,i.o_id,p.p_name,i.i_count,r.reason,u.u_id as buyerId,u.username,r.r_state " +
                "from refund as r join item as i on r.i_id=i.i_id " +
                "join product as p on i.p_id=p.p_id " +
                "join user as u on r.u_id=u.u_id  " +
                "where p.u_id=?;";
        return BaseDaoUtil.executeQuery(RefundDetail.class,sql,uId);
    }

    @Override
    public Integer updateRefundState(int iid, int state) throws SQLException {
        String sql = "update refund set r_state=? where i_id=?;";
        return BaseDaoUtil.executeUpdate(sql,state,iid);
    }
}
